package com.jsaas.sys.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.aop.Duang;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jsaas.controller.base.BaseController;
import com.jsaas.model.Permission;
import com.jsaas.model.Role;
import com.jsaas.sys.service.RolePermissionService;
import com.jsaas.sys.service.UserRoleService;

/**   
 * @Title: ActionkeyController.java 
 * @Package com.jsaas.sys.controller 
 * @Description: TODO(角色) 
 * @author tuozq 
 * @date 2017年12月15日 上午11:12:38 
 * @version V1.0   
 */
public class RoleController extends BaseController {

	public void index(){
		render("listRole.html");
	}

	public void edit(){
		String id = getPara("id");
		if(StrKit.notBlank(id)){
			setAttr("role", Role.dao.findById(id));
		}else{
			setAttr("role", new Role());
		}
		render("editRole.html");
	}

	/**
	 * 编辑角色权限
	 */
	public void editRolePerm(){
		String roleId = getPara("roleId");
		Map<String, List<Kv>> results = new HashMap<String, List<Kv>>();
		//获取所有已启用的权限项
		SqlPara sqlPara = Db.getSqlPara("rolepermission.findPermissionJoinRoleId", Kv.by("roleId", roleId).set("enable", "1"));
		List<Permission> allPermissions = Permission.dao.find(sqlPara);
		for (Permission perm : allPermissions) {
			if(results.containsKey(perm.getActionModule())){//根据功能模块分组展示
				List<Kv> list = results.get(perm.getActionModule());
				list.add(Kv.by("id", perm.getId()).set("desc", perm.getActionDesc()).set("checked", StrKit.notBlank(perm.getStr("rolepermid"))));
			}else{
				List<Kv> list = new ArrayList<Kv>();
				list.add(Kv.by("id", perm.getId()).set("desc", perm.getActionDesc()).set("checked", StrKit.notBlank(perm.getStr("rolepermid"))));
				results.put(perm.getActionModule(), list);
			}

		}
		setAttr("roleId", roleId);
		setAttr("permissionMap", results);
		render("editRolePerm.html");
	}
	
	/**
	 * 保存角色权限
	 */
	@Before(Tx.class)
	public void saveRolePerm(){
		String roleId = getPara("roleId");
		String permissionIds = getPara("permissionIds");
		Ret ret = Duang.duang(RolePermissionService.class).save(roleId, permissionIds);
		renderJson(ret);
	}

	@Before(Tx.class)
	public void save(){
		Role model = getModel(Role.class, "role");
		model.save();
		renderJson(Ret.ok());
	}

	public void query(){
		Page<Role> pagedata = Role.dao.pageinit(initPageParams(getRequest(), "role.findRole"));
		renderJson(pagedata);
	}
	
	/**
	 * 删除角色
	 * 同步删除角色和权限的关联记录
	 */
	@Before(Tx.class)
	public void delete(){
		String id = getPara("id");
		Role.dao.deleteById(id);
		//删除角色和权限的关联记录
		Duang.duang(RolePermissionService.class).deleteByRoleId(id);
		Duang.duang(UserRoleService.class).deleteByRoleId(id);
		renderJson(Ret.ok());
	}

}
